package com.example.woke;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.woke.adapter.Myclearadapter;
import com.woke.data.Datas_clear;
import com.woke.diyview.XListView;
import com.woke.diyview.XListView.IXListViewListener;
import com.woke.method.MyApp;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class MyText extends Activity implements OnItemClickListener, IXListViewListener{

	private XListView mListView;
	private MyApp application;
	private ArrayList<Datas_clear> datas_list = new ArrayList<Datas_clear>();
	private Myclearadapter adapter;
	private LayoutInflater inflater;
	private int pageno = 1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mywalletclear);
		inflater = getLayoutInflater();
		application = (MyApp) getApplication();
		intview();
		
		
	}

	private void intview() {
		mListView = (XListView) findViewById(R.id.userinfo_clear_listview);
		mListView.setPullLoadEnable(true);
		mListView.setXListViewListener(this);
		mListView.setOnItemClickListener(this);
		adapter = new Myclearadapter(inflater, datas_list);
		mListView.setAdapter(adapter);
		/*findViewById(R.id.userinfo_xia_back).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});*/
		getdata();
		
	}
	
	private void getdata() {
		AsyncHttpClient ahc = new AsyncHttpClient();
		RequestParams params = new RequestParams();
		PersistentCookieStore myCookieStore = new PersistentCookieStore(MyText.this);
		ahc.setCookieStore(myCookieStore);
//		http://app.88china.com:8384/index.php?g=api&m=Scancode&a=getPayLog&userId=1391
		params.put("g", "api");
		params.put("m", "Scancode");
		params.put("a", "getPayLog");
		params.put("userId", "1391");
		params.put("page", pageno);
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				System.err.println("page------------------"+pageno);
				try {
					String flag = response.getString("flag");
					if(flag.equals("success")){
						
						JSONObject jsonObject = response.getJSONObject("data");
						JSONObject jsonObject2 = jsonObject.getJSONObject("data");
						String json = jsonObject2.getString("list");
						
						System.out.println("json------------------"+json);
//						ArrayList<Datas_clear> parseArray = (ArrayList<Datas_clear>) JSONArray.parseArray(json, Datas_clear.class);
						ArrayList<Datas_clear> parseArray = (ArrayList<Datas_clear>) com.alibaba.fastjson.JSONArray.parseArray(json, Datas_clear.class);

						System.out.println("datalist--------------"+parseArray);
						datas_list.addAll(parseArray);
//						System.err.println("alllist------------"+alllist.addAll(datalist));
						adapter.notifyDataSetChanged();
						pageno=pageno+1;
						System.out.println("pageno"+pageno);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
			}
			@Override
			public void onStart() {
				super.onStart();
			}
			@Override
			public void onFinish() {
				super.onFinish();
			}
		});
	}
    



	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		pageno = 1;
		getdata();
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		getdata();
	}

	@Override
	public void onItemClick(AdapterView<?> adapterview, View view, int i, long l) {
		// TODO Auto-generated method stub
		
	}
	
}
